package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Category;
import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.CategoryService;

import com.starwish.starwishblog.vo.CategoryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("/categorylist")
    public ResultResponse<PageInfo<CategoryVo>> clist(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                      @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                      String name){
        ResultResponse<PageInfo<CategoryVo>> result = null;

        try {
            PageInfo<CategoryVo> rolePageInfo = categoryService.showCategoryByPage(now, size, name);
            result = new ResultResponse<>(200,"查询类别成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询类别失败！");
        }
        return result;

    }

    @DeleteMapping("/delcatecory/{id}")
    public ResultResponse<Void> delCatecoryById(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;

        try {
            categoryService.removeBykey(id);
            result = new ResultResponse<>(200,"删除类别成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除类别失败!");
        }
        return result ;
    }

    @PostMapping("/addCate")
    @ApiOperation(value = "添加")
    public ResultResponse<Void> addCategory(@RequestBody Category category){
        ResultResponse<Void> result = null;

        try {
            category.setCreateTime(new Date());
            category.setModifyTime(new Date());
            category.setIsDelete(0);
            categoryService.addCate(category);
            result = new ResultResponse<>(200,"添加类别成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加类别失败!");
        }
        return result ;

    }
    @GetMapping("/hotcate")
    @ApiOperation(value = "热门/取消热门板块!")
    public ResultResponse<Void> hotcate(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null;
        try {
            int i = categoryService.hotCateById(id,status);
            result = new ResultResponse<>(200,"推荐热门成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"推荐热门失败!");
        }
        return result;
    }

    @PostMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Category category){
        ResultResponse<Void> result= null;

        try {
            categoryService.modifyCate(category);
            category.setModifyTime(new Date());
            result=new ResultResponse<>(200,"修改类别成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改类别失败");
        }
        return  result;
    }
   @ GetMapping("/findAll")
    public ResultResponse<List<CategoryVo>> findAll(){

        ResultResponse<List<CategoryVo>> result = null;

        try {
           List<CategoryVo> cats = categoryService.findAll();
            result = new ResultResponse<>(200,"查询成功!",cats);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!!");
        }
        return result ;

    }





}
